package com.itheima.service.mongo.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.db.sql.Order;
import com.itheima.domain.db.PageBeanVo;
import com.itheima.domain.mongo.*;
import com.itheima.service.mongo.MovementService;
import com.itheima.util.ConstantUtil;
import com.sun.prism.shader.AlphaOne_RadialGradient_AlphaTest_Loader;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.awt.image.AreaAveragingScaleFilter;
import java.util.ArrayList;
import java.util.List;

@DubboService
public class MovementServiceImpl implements MovementService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private RedisIdService redisIdService;

    @Override
    public void saveMovement(Movement movement) {

        Long pid = redisIdService.getNextId(ConstantUtil.MOVEMENT_ID);

        movement.setPid(pid);

        mongoTemplate.save(movement);

        MyMovement myMovement = new MyMovement();

        myMovement.setCreated(movement.getCreated());

        myMovement.setPublishId(movement.getId());

        mongoTemplate.save(myMovement, ConstantUtil.MOVEMENT_MINE + movement.getUserId());

        Query query = new Query(Criteria.where("userId").is(movement.getUserId()));

        List<Friend> friendList = mongoTemplate.find(query, Friend.class);

        if (CollectionUtil.isNotEmpty(friendList)) {

            for (Friend friend : friendList) {

                Long friendId = friend.getFriendId();

                FriendMovement friendMovement = new FriendMovement();

                friendMovement.setPublishId(movement.getId());

                friendMovement.setCreated(movement.getCreated());

                friend.setUserId(movement.getUserId());

                mongoTemplate.save(friendMovement, ConstantUtil.MOVEMENT_FRIEND + friendId);
            }
        }
    }

    @Override
    public PageBeanVo findMyMovementByPage(Long userId, Integer pageNum, Integer pageSize) {

        Integer index = (pageNum - 1) * pageSize;

        Query query = new Query().with(Sort.by(Sort.Order.desc("created"))).skip(index).limit(pageSize);

        List<MyMovement> myMovements = mongoTemplate.find(query, MyMovement.class, ConstantUtil.MOVEMENT_MINE + userId);

        List<Movement> movementList = new ArrayList<>();

        for (MyMovement myMovement : myMovements) {

            ObjectId publishId = myMovement.getPublishId();

            Movement byId = mongoTemplate.findById(publishId, Movement.class);

            if (byId.getState() == 1) {

                movementList.add(byId);
            }
        }

        long count = mongoTemplate.count(query, MyMovement.class, ConstantUtil.MOVEMENT_MINE + userId);

        return new PageBeanVo(pageNum, pageSize, count, movementList);
    }

    @Override
    public PageBeanVo findFriendMovementByPage(Long userId, Integer pageNum, Integer pageSize) {

        Query query = new Query().with(Sort.by(Sort.Order.desc("created"))).skip((pageNum - 1) * pageSize).limit(pageSize);

        List<FriendMovement> friendMovements = mongoTemplate.find(query, FriendMovement.class, ConstantUtil.MOVEMENT_FRIEND + userId);

        List<Movement> movementList = new ArrayList<>();

        for (FriendMovement friendMovement : friendMovements) {

            Movement byId = mongoTemplate.findById(friendMovement.getPublishId(), Movement.class);

            if (byId.getState() == 1) {

                movementList.add(byId);
            }
        }

        long count = mongoTemplate.count(query, FriendMovement.class, ConstantUtil.MOVEMENT_FRIEND + userId);

        PageBeanVo pageBeanVo = new PageBeanVo(pageNum, pageSize, count, movementList);

        return pageBeanVo;
    }

    @Override
    public PageBeanVo findRecommendMovementByPage(Long userId, Integer pageNum, Integer pageSize) {

        Query query = new Query(Criteria.where("userId").is(userId)).with(Sort.by(Sort.Order.desc("score"))).skip((pageNum - 1) * pageSize).limit(pageSize);

        List<RecommendMovement> recommendMovements = mongoTemplate.find(query, RecommendMovement.class);

        List<Movement> movementList = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(recommendMovements)) {

            for (RecommendMovement recommendMovement : recommendMovements) {

                Movement movement = mongoTemplate.findById(recommendMovement.getPublishId(), Movement.class);

                if (movement.getState() == 1) {

                    movementList.add(movement);
                }
            }
        }

        long counts = mongoTemplate.count(query, RecommendMovement.class);

        return new PageBeanVo(pageNum, pageSize, counts, movementList);
    }

    @Override
    public Movement findById(ObjectId id) {

        return mongoTemplate.findById(id,Movement.class);
    }
}
